package com.powerunion.datacollection.common;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

public class Format {

	private static Format instance = null;

	private Format() {
	}

	public static Format getInstance() {
		if (instance == null)
			instance = new Format();
		return instance;
	}

	public String uniteDate(String ymd, String hour, String min) {
		StringBuffer bf = new StringBuffer();
		return bf.append(ymd).append(" ").append(hour).append(":").append(min)
				.toString();
	}

	public String replaceAllEX(String a_sOrg, String a_sOld, String a_sRep) {
		StringBuffer l_sb = new StringBuffer();
		int i = a_sOld.length();
		for (int j = 0; (j = a_sOrg.indexOf(a_sOld)) != -1;) {
			l_sb.append(a_sOrg.substring(0, j) + a_sRep);
			a_sOrg = a_sOrg.substring(i + j);
		}

		l_sb.append(a_sOrg);
		return l_sb.toString();
	}

	public String Replace(String source, String oldString, String newString) {
		StringBuffer output = new StringBuffer();
		int lengthOfSource = source.length();
		int lengthOfOld = oldString.length();
		int posStart;
		int pos;
		for (posStart = 0; (pos = source.indexOf(oldString, posStart)) >= 0; posStart = pos
				+ lengthOfOld) {
			output.append(source.substring(posStart, pos));
			output.append(newString);
		}

		if (posStart < lengthOfSource) {
			output.append(source.substring(posStart));
		}
		return output.toString();
	}

	public Date stringToDate(String str) {
		String strFormat = "yyyy-MM-dd HH:mm";
		if (str != null && str.length() == 10) {
			strFormat = "yyyy-MM-dd";
		}
		SimpleDateFormat sdFormat = new SimpleDateFormat(strFormat);
		Date date = new Date();
		try {
			date = sdFormat.parse(str);
		} catch (Exception e) {
			Date date1 = null;
			return date1;
		}
		return date;
	}

	public Date stringToDate(String str, String strFormat) {
		if (StringUtils.isEmpty(str)) {
			return null;
		}
		SimpleDateFormat sdFormat = new SimpleDateFormat(strFormat);
		Date date = new Date();
		try {
			date = sdFormat.parse(str);
		} catch (Exception e) {
			Date date1 = null;
			return date1;
		}
		return date;
	}

	public String dateToYMD(Date dt) {
		SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd");
		String str = "";
		try {
			str = sdFormat.format(dt);
		} catch (Exception e) {
			String s = "";
			return s;
		}
		if (str.equals("1900-01-01")) {
			str = "";
		}
		return str;
	}

	public String dateToYMD2(Date dt) {
		SimpleDateFormat sdFormat = new SimpleDateFormat("yyyyMMdd");
		String str = "";
		try {
			str = sdFormat.format(dt);
		} catch (Exception e) {
			String s = "";
			return s;
		}
		if (str.equals("19000101")) {
			str = "";
		}
		return str;
	}

	public String dateToString(Date dt) {
		SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String str = "";
		try {
			str = sdFormat.format(dt);
		} catch (Exception e) {
			String s = "";
			return s;
		}
		if (str.equals("1900-01-01 00:00")) {
			str = "";
		}
		return str;
	}

	public String dateToString(Date dt, String strFormat) {
		SimpleDateFormat sdFormat = new SimpleDateFormat(strFormat);
		String str = "";
		try {
			str = sdFormat.format(dt);
		} catch (Exception e) {
			String s = "";
			return s;
		}
		if (str.equals("1900-01-01 00:00")) {
			str = "";
		}
		return str;
	}

	public static String getStringYM(Date dt) {
		SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM");
		String str = "";
		try {
			str = sdFormat.format(dt);
		} catch (Exception e) {
			String s = "";
			return s;
		}
		if (str.equals("1900-01-01 00:00")) {
			str = "";
		}
		return str;
	}

	public Date nextDay(Date dt) {
		Date nextDay = null;
		nextDay = new Date(dt.getYear(), dt.getMonth(), dt.getDate() + 1);
		return nextDay;
	}

	public Date dayAfter(Date dt, int days) {
		Date nextDay = null;
		nextDay = new Date(dt.getYear(), dt.getMonth(), dt.getDate() + days);
		return nextDay;
	}

	public Date dayBefore(Date dt, int days) {
		Date nextDay = null;
		nextDay = new Date(dt.getYear(), dt.getMonth(), dt.getDate() - days);
		return nextDay;
	}

	public Date dayAfter(Date dt, int interval, int format) {
		Date nextDay = null;
		switch (format) {
		case 1: // '\001'
			nextDay = new Date(dt.getYear() + interval, dt.getMonth(), dt
					.getDate(), dt.getHours(), dt.getMinutes(), dt.getSeconds());
			break;

		case 2: // '\002'
			nextDay = new Date(dt.getYear(), dt.getMonth() + interval, dt
					.getDate(), dt.getHours(), dt.getMinutes(), dt.getSeconds());
			break;

		case 3: // '\003'
			nextDay = new Date(dt.getYear(), dt.getMonth(), dt.getDate()
					+ interval, dt.getHours(), dt.getMinutes(), dt.getSeconds());
			break;

		case 4: // '\004'
			nextDay = new Date(dt.getYear(), dt.getMonth(), dt.getDate(), dt
					.getHours()
					+ interval, dt.getMinutes(), dt.getSeconds());
			break;

		case 5: // '\005'
			nextDay = new Date(dt.getYear(), dt.getMonth(), dt.getDate(), dt
					.getHours(), dt.getMinutes() + interval, dt.getSeconds());
			break;

		case 6: // '\006'
			nextDay = new Date(dt.getYear(), dt.getMonth(), dt.getDate(), dt
					.getHours(), dt.getMinutes(), dt.getSeconds() + interval);
			break;
		}
		return nextDay;
	}

	public String dateToWeek(Date dt) {
		int iDay = dt.getDay();
		String temp = "";
		switch (iDay) {
		case 0: // '\0'
			temp = "\u661F\u671F\u5929";
			break;

		case 1: // '\001'
			temp = "\u661F\u671F\u4E00";
			break;

		case 2: // '\002'
			temp = "\u661F\u671F\u4E8C";
			break;

		case 3: // '\003'
			temp = "\u661F\u671F\u4E09";
			break;

		case 4: // '\004'
			temp = "\u661F\u671F\u56DB";
			break;

		case 5: // '\005'
			temp = "\u661F\u671F\u4E94";
			break;

		case 6: // '\006'
			temp = "\u661F\u671F\u516D";
			break;

		default:
			temp = "";
			break;
		}
		return temp;
	}

	/**
	 * 计算某个确切时间
	 * @param timeStr 时间
	 * @param days 天数
	 * @return
	 */
	public String exactlyDate(String timeStr, String days) {
		if (StringUtils.isEmpty(timeStr)) {
			return "";
		}

		if (StringUtils.isEmpty(days)) {
			return "";
		}

		timeStr = this.formatTime(timeStr);
		String dateFormat = "yyyy-MM-dd HH:mm:ss";
		Date date = this.stringToDate(timeStr, dateFormat);
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);

		cal.add(GregorianCalendar.DATE, Integer.parseInt(days));//在日期上加7天
		Date resultDate = cal.getTime();
		return this.dateToString(resultDate, dateFormat);

	}

	public int interval(Date first, Date last) {
		int inter = 0;
		Date dt_first = stringToDate(dateToYMD(first));
		Date dt_last = stringToDate(dateToYMD(last));
		inter = (int) ((dt_last.getTime() - dt_first.getTime()) / 0x5265c00L);
		return inter;
	}

	public String trunc(String str, int count) {
		if (str == null) {
			return "";
		}
		String temp = str;
		int j = 0;
		int k = 0;
		int i = 0;
		i = 0;
		try {
			do {
				if (i >= str.length()) {
					break;
				}
				if (temp.charAt(i) >= '!' && temp.charAt(i) <= '\200') {
					j++;
				} else {
					k++;
				}
				if (k + j / 2 + j % 2 > count) {
					break;
				}
				i++;
			} while (true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (temp.length() > i)
			return temp.substring(0, i) + "...";
		return temp.substring(0, i);
	}

	private Map map = new HashMap();

	public int getToDayNo(int i) {
		Integer ig = new Integer(i);
		if (map.containsKey(ig)) {
			Object obj = map.get(ig);
			map.put(ig, new Integer(Integer.parseInt(obj.toString()) + 1));
			return Integer.parseInt(map.get(ig).toString());
		}
		map.put(ig, new Integer(1));
		return 1;
	}

	public String getInvert(Date first, Date second) {
		StringBuffer sb = new StringBuffer();
		long invert = second.getTime() - first.getTime();
		int hours = 0;
		int minutes = 0;
		int seconds = 0;
		int days = (int) ((invert / 24L) * 3600L * 1000L);
		long temp = (invert % 24L) * 3600L * 1000L;
		if (temp != 0L) {
			hours = (int) ((temp / 3600L) * 1000L);
			temp = (temp % 3600L) * 1000L;
			if (temp != 0L) {
				minutes = (int) ((temp / 60L) * 1000L);
				temp = (temp % 60L) * 1000L;
				if (temp != 0L) {
					seconds = (int) (temp / 1000L);
				}
			}
		}
		return sb.append(days).append("Days").append(hours).append(":").append(
				minutes).append(":").append(seconds).toString();
	}

	/**
	 * 用于转换extjs日期控件时间的格式
	 * 该日期控件得到的时间会含有T字符
	 * @param dataTime
	 * @return
	 */
	public String formatTime(String dataTime) {

		if (dataTime == null || dataTime.equals("") || dataTime.length() < 8) {
			return "";
		}

		dataTime = dataTime.trim();
		dataTime = dataTime.replaceAll("T", " ");

		return dataTime;
	}

	public int daysBetween(Date date1, Date date2) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date1);
		long time1 = cal.getTimeInMillis();
		cal.setTime(date2);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	public static void main(String[] args) {
		String timeStr = "2010-10-21 10:01:22";
		String days = "0";
		Date date = Format.getInstance().stringToDate(timeStr);
		Date date2 = new Date();
		int count =  Format.getInstance().daysBetween(date,date2);
		System.out.println("count:" + count);
	}
}
